/*
 * Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package at.ssw.visualizer.bc.options;

import at.ssw.visualizer.bc.model.BytecodeModel;
import at.ssw.visualizer.bc.modelimpl.BytecodeModelImpl;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.openide.util.Lookup;

/**
 * The panel for setting the classpaths.
 *
 * @author Alexander Reder
 * @author Christian Wimmer
 */
final class BCOptionPanel extends javax.swing.JPanel {

    private final BCOptionsPanelController controller;
    private final BytecodeModelImpl bytecodeModel;
    private final DefaultListModel listModel;

    public BCOptionPanel(BCOptionsPanelController controller) {
        this.controller = controller;
        initComponents();

        bytecodeModel = (BytecodeModelImpl) Lookup.getDefault().lookup(BytecodeModel.class);
        listModel = new DefaultListModel();
        classpathList.setModel(listModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        classpathScrollPane = new javax.swing.JScrollPane();
        classpathList = new javax.swing.JList();
        addPathButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        addJarZipButton = new javax.swing.JButton();
        addDefaultClasspath = new javax.swing.JButton();

        classpathList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        classpathScrollPane.setViewportView(classpathList);

        org.openide.awt.Mnemonics.setLocalizedText(addPathButton, "Add folder");
        addPathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPathButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(removeButton, "Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(addJarZipButton, "Add Jar/Zip file");
        addJarZipButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addJarZipButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(addDefaultClasspath, "Add default classpath");
        addDefaultClasspath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDefaultClasspathActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(classpathScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(addJarZipButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addDefaultClasspath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addPathButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addDefaultClasspath, addJarZipButton, removeButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addPathButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addJarZipButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addDefaultClasspath)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeButton))
                    .addComponent(classpathScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addDefaultClasspathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDefaultClasspathActionPerformed
        for (String cp : bytecodeModel.getDefaultClassPaths()) {
            listModel.addElement(cp);
            controller.changed();
            classpathList.setSelectedIndex(listModel.getSize() - 1);
        }
    }//GEN-LAST:event_addDefaultClasspathActionPerformed

    private void addJarZipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJarZipButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter(new FileNameExtensionFilter("Bytecode archives", "jar", "zip"));
        if (chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION) {
            listModel.addElement(chooser.getSelectedFile().getAbsolutePath());
            controller.changed();
            classpathList.setSelectedIndex(listModel.getSize() - 1);
        }
    }//GEN-LAST:event_addJarZipButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        int idx = classpathList.getSelectedIndex();
        if (idx >= 0 && idx < listModel.getSize()) {
            listModel.remove(idx);
            controller.changed();
            if (listModel.size() > 0) {
                classpathList.setSelectedIndex(Math.max(0, idx - 1));
            }
        }
    }//GEN-LAST:event_removeButtonActionPerformed

    private void addPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPathButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION) {
            listModel.addElement(chooser.getSelectedFile().getAbsolutePath());
            controller.changed();
            classpathList.setSelectedIndex(listModel.getSize() - 1);
        }
    }//GEN-LAST:event_addPathButtonActionPerformed

    public void update() {
        listModel.clear();
        for (String cp : bytecodeModel.getClassPaths()) {
            listModel.addElement(cp);
        }
    }

    public void applyChanges() {
        String[] data = new String[listModel.getSize()];
        listModel.copyInto(data);
        bytecodeModel.setClassPaths(data);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addDefaultClasspath;
    private javax.swing.JButton addJarZipButton;
    private javax.swing.JButton addPathButton;
    private javax.swing.JList classpathList;
    private javax.swing.JScrollPane classpathScrollPane;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables
}
